#include "../Header/Game.h"
#include "../Header/Core/Device.h"
#include "../Header/State/Logo.h"

Game* Game::m_Instance = 0;

Game::Game()
: m_IsPause(false), m_IsAlive(true)
{

}

Game::~Game()
{
	m_IsPause = false;
	m_IsAlive = false;

}

void Game::Pause()
{
	if (!m_IsPause)
		m_IsPause = true;
}

void Game::Resume()
{
	if (m_IsPause)
		m_IsPause = false;
}

void Game::Exit()
{
	if (m_IsAlive)
		m_IsAlive = false;
}

void Game::Destroy()
{
#if CURRENT_DBG_MODE == DBG_BASE
	Device::GetInstance()->LogEx("Game state: Destroy\n");
#endif
	Device::DestroyInstance();
	StateManager::DestroyInstance();
}

void Game::Update(__float_ deltaTime)
{
}

void Game::Draw()
{
}

void Game::Touch(POINT position, __bool_ bStatus)
{
	Vector2 mousePos = Vector2((__float_)position.x, (__float_)position.y);
}

#if CURRENT_PLATFORM == WIN

void Game::RegisterViewUpdateFunc(__bool_ (*viewUpdateFunc)())
{
	m_ViewUpdateFunc = viewUpdateFunc;
}

void Game::Key(unsigned char key, __bool_ bStatus)
{
	switch (key)
	{
	default:
		break;
	}
}

#endif

__int32_ Game::Start()
{
#if CURRENT_DBG_MODE == DBG_BASE
	Device::GetInstance()->LogEx("Game state: Start\n");
#endif
	Logo* gameLogo = new Logo();
	StateManager::GetInstance()->AddState(gameLogo);
	StateManager::GetInstance()->SwitchState(gameLogo);
	return 0;
}

void Game::Run(__float_ deltaTime)
{
#if CURRENT_DBG_MODE == DBG_BASE
	Device::GetInstance()->LogEx("Game state: Run\n");
#endif
	while (m_IsAlive)
	{
		/*Update(deltaTime);
		Draw();*/
		if (!m_IsPause)
		{
			StateManager::GetInstance()->Update(deltaTime, false);
		}
		else //Is pause
		{
			StateManager::GetInstance()->Update(deltaTime, true);
		}
#if CURRENT_PLATFORM == WIN
		if (this->m_ViewUpdateFunc())
			Exit();
#endif
		Device::GetInstance()->SleepEx(0);
	};
	Destroy();
}